package android.meetingatsb

import android.content.Intent
import android.meetingatsb.common.Dating
import android.meetingatsb.common.FontAwesome
import android.meetingatsb.common.Requirement
import android.meetingatsb.location.NewMapFragment
import android.os.Bundle
import android.support.design.widget.NavigationView
import android.support.v4.view.GravityCompat
import android.support.v7.app.ActionBarDrawerToggle
import android.support.v7.app.AppCompatActivity
import android.util.Log
import android.view.MenuItem
import android.view.View
import android.widget.Button
import android.widget.EditText
import android.widget.TextView
import kotlinx.android.synthetic.main.activity_main.*


class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelectedListener {

    private lateinit var editDating: Dating
    private lateinit var editRequirement: Requirement

    private lateinit var datingType: EditText
    private lateinit var datingDetail: EditText

    private lateinit var datingGroup: EditText
    private lateinit var datingTime: EditText
    private lateinit var datingAddition: EditText
    private lateinit var submitDating: Button

    lateinit var mapFragment: NewMapFragment

    private var datingLocation: String? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        bindWidgets()
        setListener()
        setFragment()
        setListener()
        setData()
    }

    private fun setFragment() {
        val fragTran = fragmentManager.beginTransaction()
        mapFragment = NewMapFragment()
        fragTran.replace(R.id.map_fragment, mapFragment)
        fragTran.commit()
    }

    private fun setListener() {
        setSupportActionBar(toolbar)
        val toggle = ActionBarDrawerToggle(
                this, drawer_layout, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close)
        drawer_layout.addDrawerListener(toggle)
        toggle.syncState()
        (findViewById<EditText>(R.id.dating_location_keyword)).onFocusChangeListener = View.OnFocusChangeListener { v, hasFocus ->
            if (!hasFocus) {
                val keyword = (v as EditText).text.toString()
                mapFragment.getDatingLocation(keyword)
            }
        }
        findViewById<Button>(R.id.submit_dating).setOnClickListener {
            editDating = Dating(datingType.text.toString(),
                    datingDetail.text.toString(),
                    "匹配中")
            Log.i("123", datingGroup.text.toString())
            editRequirement = Requirement(datingGroup.text.toString(),
                    datingLocation,
                    datingTime.text.toString(),
                    datingAddition.text.toString())

//          Intent = Intent(this, MatchWaitingActivity::class.java)
            intent = Intent(this, MatchSuccessActivity::class.java)
            val matchBundle = Bundle()
            matchBundle.putParcelable("dating", editDating)
            matchBundle.putParcelable("selfRequirement", editRequirement)
            intent.putExtras(matchBundle)
            startActivity(intent)
        }
        nav_view.setNavigationItemSelectedListener(this)

    }

    private fun bindWidgets() {
        datingType = findViewById(R.id.dating_type)
        datingDetail = findViewById(R.id.dating_detail)

        datingGroup = findViewById(R.id.group)
        datingTime = findViewById(R.id.time)
        datingAddition = findViewById(R.id.addition)

        submitDating = findViewById(R.id.submit_dating)

    }

    private fun setData() {
        (findViewById<TextView>(R.id.dating_type_icon)).typeface = FontAwesome.getFont(this)
        (findViewById<TextView>(R.id.group_icon)).typeface = FontAwesome.getFont(this)
        (findViewById<TextView>(R.id.time_icon)).typeface = FontAwesome.getFont(this)
        (findViewById<TextView>(R.id.addition_icon)).typeface = FontAwesome.getFont(this)
    }

    override fun onBackPressed() {
        if (drawer_layout.isDrawerOpen(GravityCompat.START)) {
            drawer_layout.closeDrawer(GravityCompat.START)
        } else {
            super.onBackPressed()
        }
    }


    override fun onNavigationItemSelected(item: MenuItem): Boolean {
        when (item.itemId) {
            R.id.watch_dating_record -> {
            }
            R.id.watch_user_info -> {
                intent = Intent(this, UserInfoActivity::class.java)

            }
            R.id.login -> {
                intent = Intent(this, LoginActivity::class.java)
            }
        }
        drawer_layout.closeDrawer(GravityCompat.START)
        startActivity(intent)
        return true
    }


    fun setLocation(datingLocation: String?) {
        this.datingLocation = datingLocation
    }
}
